package com.ysd.lis.service.bac.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.sta.StaBreakPointDto;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.bac.*;
import com.ysd.lis.entity.lab.LabMaininfo;
import com.ysd.lis.entity.sta.StaBreakPoint;
import com.ysd.lis.mapper.bac.BacBreakPointLogMapper;
import com.ysd.lis.mapper.bac.BacBreakPointMapper;
import com.ysd.lis.mapper.bac.BacGermDictMapper;
import com.ysd.lis.request.Params;
import com.ysd.lis.request.bac.BreakPointDto;
import com.ysd.lis.request.bac.BreakPointMapperCategoryDto;
import com.ysd.lis.request.bac.BreakPointMapperCategoryParam;
import com.ysd.lis.request.bac.BreakPointParam;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.service.bac.BacBreakPointLogService;
import com.ysd.lis.service.bac.BacBreakPointService;
import com.ysd.util.ToolsUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wanghuaixing
 * @since 2024-12-25
 */
@Service
public class BacBreakPointServiceImpl extends ServiceImpl<BacBreakPointMapper, BacBreakPoint> implements BacBreakPointService {

    @Autowired
    BacBreakPointMapper bacBreakPointMapper;
    @Autowired
    BacGermDictMapper bacGermDictMapper;
    @Autowired
    SysBasDictDetailService sysBasDictDetailService;
    @Autowired
    BacBreakPointLogService bacBreakPointLogService;
    @Autowired
    BacBreakPointLogMapper bacBreakPointLogMapper;

    @Override
    public Result findBreakPointList(Params params) {
        Page<BacBreakPoint> page = new Page<>();
        page.setCurrent(params.getPageIndex());
        page.setSize(params.getPageSize());

        LambdaQueryWrapper<BacBreakPoint> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BacBreakPoint::getDelFlag, 0);
        if (!StringUtils.isEmpty(params.getSearchValue())) {
            queryWrapper.and(wrapper -> wrapper.like(BacBreakPoint::getOrganismCode, params.getSearchValue()).or().eq(BacBreakPoint::getId,  params.getSearchValue()));
        }

        Page<BacBreakPoint> bacBreakPointPage = bacBreakPointMapper.selectPage(page, queryWrapper);
        return Result.succ(1, "查询成功", bacBreakPointPage);
    }

    @Override
    public Result findBreakPointOne(BreakPointParam params) {
        LambdaQueryWrapper<BacBreakPoint> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BacBreakPoint::getDelFlag, 0);
        queryWrapper.eq(BacBreakPoint::getId, params.getBreakPointId());
        BacBreakPoint entry = bacBreakPointMapper.selectOne(queryWrapper);
        return Result.succ(1, "查询成功", entry);
    }

    @Override
    public Result addBreakPoint(BacBreakPoint bacBreakPoint) {
        //判断 isGenus 是否设置属
        if (ToolsUtils.isNotEmpty(bacBreakPoint.getIsGenus()) && bacBreakPoint.getIsGenus() == 1) {
            //则查出此细菌的属然后设置属属性，并将细菌字段置空
            String whonetOrgCode = bacBreakPoint.getWhonetOrgCode();
            LambdaQueryWrapper<BacGermDict> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BacGermDict::getGermNo, whonetOrgCode)
                    .eq(BacGermDict::getDelFlag, 0);
            List<BacGermDict> bacGermDicts = bacGermDictMapper.selectList(queryWrapper);
            if(ToolsUtils.isNotEmpty(bacGermDicts)){
                BacGermDict bacGermDict = bacGermDicts.get(0);
                String genusCode = bacGermDict.getGenusCode();
                bacBreakPoint.setGenusCode(genusCode);
                //清空折点bacBreakPoint的whonetOrgCode
                bacBreakPoint.setWhonetOrgCode(null);
            }
        }
        int i = bacBreakPointMapper.insert(bacBreakPoint);
        if (i > 0) {
            //添加日志
            BacBreakPointLog bacBreakPointLog = new BacBreakPointLog();
            BeanUtil.copyProperties(bacBreakPoint, bacBreakPointLog,  "id","creator", "editor", "createTime", "editTime", "delFlag");
            bacBreakPointLog.setContentValue(JSON.toJSONString(bacBreakPoint));
            bacBreakPointLog.setBusinessName("新增折点");
            bacBreakPointLog.setMainId(bacBreakPoint.getId());
            bacBreakPointLogService.addPointLog(bacBreakPointLog);
            return Result.succ(1, "新增成功", "");
        } else {
            return Result.fail("添加折点失败");
        }
    }

    @Override
    public Result updateBreakPoint(BacBreakPoint bacBreakPoint) {
        //添加日志
        BacBreakPoint bacBreakPoint1 = bacBreakPointMapper.selectById(bacBreakPoint.getId());
        BacBreakPointLog bacBreakPointLog = new BacBreakPointLog();
        BeanUtil.copyProperties(bacBreakPoint, bacBreakPointLog,  "id","creator", "editor", "createTime", "editTime", "delFlag");
        bacBreakPointLog.setInitialValue(JSON.toJSONString(bacBreakPoint1));
        bacBreakPointLog.setContentValue(JSON.toJSONString(bacBreakPoint));
        bacBreakPointLog.setBusinessName("编辑折点");
        bacBreakPointLog.setMainId(bacBreakPoint.getId());
        bacBreakPointLogService.addPointLog(bacBreakPointLog);

        int i = bacBreakPointMapper.updateById(bacBreakPoint);
        if (i > 0) {
            return Result.succ(1, "修改成功", "");
        } else {
            return Result.fail("修改折点失败");
        }
    }

    @Override
    public Result batchDeleteBreakPoint(BreakPointDto vo) {
        List<BacBreakPoint> bacBreakPointList = vo.getBacBreakPointList();
        //取出bacGermDictList中的id
        List<String> ids = bacBreakPointList.stream().map(BacBreakPoint::getId).collect(Collectors.toList());

        //添加日志
        List<BacBreakPoint> bacBreakPointList1 = bacBreakPointMapper.selectBatchIds(ids);
        for (BacBreakPoint bacBreakPoint : bacBreakPointList1) {
            BacBreakPointLog bacBreakPointLog = new BacBreakPointLog();
            BeanUtil.copyProperties(bacBreakPoint, bacBreakPointLog,  "id","creator", "editor", "createTime", "editTime", "delFlag");
            bacBreakPointLog.setInitialValue(JSON.toJSONString(bacBreakPoint));
            bacBreakPointLog.setBusinessName("删除折点");
            bacBreakPointLog.setMainId(bacBreakPoint.getId());
            bacBreakPointLogService.addPointLog(bacBreakPointLog);
        }
        int i = bacBreakPointMapper.deleteBatchIds(ids);
        if (i > 0) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    /*用户自定义折点 分类明细查询*/
    @Override
    public Result findCategoryByCode(BreakPointMapperCategoryParam input) {
        List<BreakPointMapperCategoryDto> res = new ArrayList<>();
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "ANAEROBE".toUpperCase().equals(input.getCateGoryCode())) {
            BreakPointMapperCategoryDto breakPointMapperCategoryDto = new BreakPointMapperCategoryDto();
            breakPointMapperCategoryDto.setCode("ANA");
            breakPointMapperCategoryDto.setName("厌氧");
            breakPointMapperCategoryDto.setCateGoryCode(input.getCateGoryCode());
            res.add(breakPointMapperCategoryDto);
            BreakPointMapperCategoryDto breakPointMapperCategoryDto1 = new BreakPointMapperCategoryDto();
            breakPointMapperCategoryDto1.setCode("");
            breakPointMapperCategoryDto1.setName("非厌氧");
            breakPointMapperCategoryDto1.setCateGoryCode(input.getCateGoryCode());
            res.add(breakPointMapperCategoryDto1);
            return Result.succ(1, "成功", res);
        }
        Map<String, List<BacGermDict>> groupList = null;
        LambdaQueryWrapper<BacGermDict> bacGermDictLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bacGermDictLambdaQueryWrapper.eq(BacGermDict::getDelFlag, 0);
        List<BacGermDict> germDicts = bacGermDictMapper.selectList(bacGermDictLambdaQueryWrapper);

        if (ToolsUtils.isEmpty(germDicts)) {
            return Result.succ(1, "成功", res);
        }


        //.collect(Collectors.groupingBy(BacTestResult::getGermNo));
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "serumGroup".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            germDicts = germDicts.stream().filter(bg -> ToolsUtils.isNotEmpty(bg.getSerumGroup())).collect(Collectors.toList());
            if (ToolsUtils.isNotEmpty(input.getFilterCondition()) && "2".equals(input.getFilterCondition())) {/*已 维护*/
                LambdaQueryWrapper<BacBreakPoint> bacBreakPointLambdaQueryWrapper = new LambdaQueryWrapper<>();
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getDelFlag, 0);
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getGuidelines, "AUser-defined");
                List<BacBreakPoint> bacBreakPoints = bacBreakPointMapper.selectList(bacBreakPointLambdaQueryWrapper);
                List<String> serumGroupList = bacBreakPoints.stream().map(BacBreakPoint::getSerovarGroup).collect(Collectors.toList());
                if (ToolsUtils.isNotEmpty(serumGroupList)) {
                    germDicts = germDicts.stream().filter(bg -> serumGroupList.contains(bg.getSerumGroup())).collect(Collectors.toList());
                } else {
                    germDicts = germDicts.stream().filter(bg -> Arrays.asList("!@#QWE").contains(bg.getSerumGroup())).collect(Collectors.toList());
                }
            }
            if (ToolsUtils.isNotEmpty(input.getFilterCondition()) && "3".equals(input.getFilterCondition())) {/*未维护 */
                LambdaQueryWrapper<BacBreakPoint> bacBreakPointLambdaQueryWrapper = new LambdaQueryWrapper<>();
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getDelFlag, 0);
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getGuidelines, "AUser-defined");
                List<BacBreakPoint> bacBreakPoints = bacBreakPointMapper.selectList(bacBreakPointLambdaQueryWrapper);
                List<String> serumGroupList = bacBreakPoints.stream().map(BacBreakPoint::getSerovarGroup).collect(Collectors.toList());
                if (ToolsUtils.isNotEmpty(serumGroupList)) {
                    germDicts = germDicts.stream().filter(bg -> !serumGroupList.contains(bg.getSerumGroup())).collect(Collectors.toList());
                }
            }
            if (ToolsUtils.isNotEmpty(input.getKeyword()) && ToolsUtils.isNotEmpty(germDicts)) {
                germDicts = germDicts.stream().filter(bg -> Arrays.asList(input.getKeyword()).contains(bg.getSerumGroup())).collect(Collectors.toList());
            }
            if (ToolsUtils.isEmpty(germDicts)) {
                return Result.succ(1, "成功", res);
            }
            groupList = germDicts.stream().collect(Collectors.groupingBy(BacGermDict::getSerumGroup));
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "WHONETORGCODE".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            germDicts = germDicts.stream().filter(bg -> ToolsUtils.isNotEmpty(bg.getGermNo())).collect(Collectors.toList());
            if (ToolsUtils.isNotEmpty(input.getFilterCondition()) && "2".equals(input.getFilterCondition())) {/*已 维护*/
                LambdaQueryWrapper<BacBreakPoint> bacBreakPointLambdaQueryWrapper = new LambdaQueryWrapper<>();
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getDelFlag, 0);
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getGuidelines, "AUser-defined");
                List<BacBreakPoint> bacBreakPoints = bacBreakPointMapper.selectList(bacBreakPointLambdaQueryWrapper);
                List<String> serumGroupList = bacBreakPoints.stream().map(BacBreakPoint::getWhonetOrgCode).collect(Collectors.toList());
                if (ToolsUtils.isNotEmpty(serumGroupList)) {
                    germDicts = germDicts.stream().filter(bg -> serumGroupList.contains(bg.getGermNo())).collect(Collectors.toList());
                } else {
                    germDicts = germDicts.stream().filter(bg -> Arrays.asList("!@#QWE").contains(bg.getGermNo())).collect(Collectors.toList());
                }
            }
            if (ToolsUtils.isNotEmpty(input.getFilterCondition()) && "3".equals(input.getFilterCondition())) {/*未维护 */
                LambdaQueryWrapper<BacBreakPoint> bacBreakPointLambdaQueryWrapper = new LambdaQueryWrapper<>();
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getDelFlag, 0);
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getGuidelines, "AUser-defined");
                List<BacBreakPoint> bacBreakPoints = bacBreakPointMapper.selectList(bacBreakPointLambdaQueryWrapper);
                List<String> serumGroupList = bacBreakPoints.stream().map(BacBreakPoint::getWhonetOrgCode).collect(Collectors.toList());
                if (ToolsUtils.isNotEmpty(serumGroupList)) {
                    germDicts = germDicts.stream().filter(bg -> !serumGroupList.contains(bg.getGermNo())).collect(Collectors.toList());
                }
            }
            if (ToolsUtils.isNotEmpty(input.getKeyword()) && ToolsUtils.isNotEmpty(germDicts)) {
                germDicts = germDicts.stream().filter(bg -> Arrays.asList(input.getKeyword()).contains(bg.getGermNo())).collect(Collectors.toList());
            }
            if (ToolsUtils.isEmpty(germDicts)) {
                return Result.succ(1, "成功", res);
            }
            groupList = germDicts.stream().collect(Collectors.groupingBy(BacGermDict::getGermNo));
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "speciesGroup".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            germDicts = germDicts.stream().filter(bg -> ToolsUtils.isNotEmpty(bg.getSpeciesGroup())).collect(Collectors.toList());
            if (ToolsUtils.isNotEmpty(input.getFilterCondition()) && "2".equals(input.getFilterCondition())) {/*已 维护*/
                LambdaQueryWrapper<BacBreakPoint> bacBreakPointLambdaQueryWrapper = new LambdaQueryWrapper<>();
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getDelFlag, 0);
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getGuidelines, "AUser-defined");
                List<BacBreakPoint> bacBreakPoints = bacBreakPointMapper.selectList(bacBreakPointLambdaQueryWrapper);
                List<String> serumGroupList = bacBreakPoints.stream().map(BacBreakPoint::getSpeciesGroup).collect(Collectors.toList());
                if (ToolsUtils.isNotEmpty(serumGroupList)) {
                    germDicts = germDicts.stream().filter(bg -> serumGroupList.contains(bg.getSpeciesGroup())).collect(Collectors.toList());
                } else {
                    germDicts = germDicts.stream().filter(bg -> Arrays.asList("!@#QWE").contains(bg.getSpeciesGroup())).collect(Collectors.toList());
                }
            }
            if (ToolsUtils.isNotEmpty(input.getFilterCondition()) && "3".equals(input.getFilterCondition())) {/*未维护 */
                LambdaQueryWrapper<BacBreakPoint> bacBreakPointLambdaQueryWrapper = new LambdaQueryWrapper<>();
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getDelFlag, 0);
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getGuidelines, "AUser-defined");
                List<BacBreakPoint> bacBreakPoints = bacBreakPointMapper.selectList(bacBreakPointLambdaQueryWrapper);
                List<String> serumGroupList = bacBreakPoints.stream().map(BacBreakPoint::getSpeciesGroup).collect(Collectors.toList());
                if (ToolsUtils.isNotEmpty(serumGroupList)) {
                    germDicts = germDicts.stream().filter(bg -> !serumGroupList.contains(bg.getSpeciesGroup())).collect(Collectors.toList());
                }
            }
            if (ToolsUtils.isNotEmpty(input.getKeyword()) && ToolsUtils.isNotEmpty(germDicts)) {
                germDicts = germDicts.stream().filter(bg -> Arrays.asList(input.getKeyword()).contains(bg.getSpeciesGroup())).collect(Collectors.toList());
            }
            if (ToolsUtils.isEmpty(germDicts)) {
                return Result.succ(1, "成功", res);
            }
            groupList = germDicts.stream().collect(Collectors.groupingBy(BacGermDict::getSpeciesGroup));
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "genusCode".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            germDicts.stream().filter(bg -> ToolsUtils.isNotEmpty(bg.getGenusCode())).collect(Collectors.toList());
            if (ToolsUtils.isNotEmpty(input.getFilterCondition()) && "2".equals(input.getFilterCondition())) {/*已 维护*/
                LambdaQueryWrapper<BacBreakPoint> bacBreakPointLambdaQueryWrapper = new LambdaQueryWrapper<>();
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getDelFlag, 0);
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getGuidelines, "AUser-defined");
                List<BacBreakPoint> bacBreakPoints = bacBreakPointMapper.selectList(bacBreakPointLambdaQueryWrapper);
                List<String> serumGroupList = bacBreakPoints.stream().map(BacBreakPoint::getGenusCode).collect(Collectors.toList());
                if (ToolsUtils.isNotEmpty(serumGroupList)) {
                    germDicts = germDicts.stream().filter(bg -> serumGroupList.contains(bg.getGenusCode())).collect(Collectors.toList());
                } else {
                    germDicts = germDicts.stream().filter(bg -> Arrays.asList("!@#QWE").contains(bg.getGenusCode())).collect(Collectors.toList());
                }
            }
            if (ToolsUtils.isNotEmpty(input.getFilterCondition()) && "3".equals(input.getFilterCondition())) {/*未维护 */
                LambdaQueryWrapper<BacBreakPoint> bacBreakPointLambdaQueryWrapper = new LambdaQueryWrapper<>();
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getDelFlag, 0);
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getGuidelines, "AUser-defined");
                List<BacBreakPoint> bacBreakPoints = bacBreakPointMapper.selectList(bacBreakPointLambdaQueryWrapper);
                List<String> serumGroupList = bacBreakPoints.stream().map(BacBreakPoint::getGenusCode).collect(Collectors.toList());
                if (ToolsUtils.isNotEmpty(serumGroupList)) {
                    germDicts = germDicts.stream().filter(bg -> !serumGroupList.contains(bg.getGenusCode())).collect(Collectors.toList());
                }
            }
            if (ToolsUtils.isNotEmpty(input.getKeyword()) && ToolsUtils.isNotEmpty(germDicts)) {
                germDicts = germDicts.stream().filter(bg -> Arrays.asList(input.getKeyword()).contains(bg.getGenusCode())).collect(Collectors.toList());
            }
            if (ToolsUtils.isEmpty(germDicts)) {
                return Result.succ(1, "成功", res);
            }
            groupList = germDicts.stream().filter(bg -> ToolsUtils.isNotEmpty(bg.getGenusCode())).collect(Collectors.groupingBy(BacGermDict::getGenusCode));
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "genusGroup".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            germDicts = germDicts.stream().filter(bg -> ToolsUtils.isNotEmpty(bg.getGenusGroup())).collect(Collectors.toList());
            if (ToolsUtils.isNotEmpty(input.getFilterCondition()) && "2".equals(input.getFilterCondition())) {/*已 维护*/
                LambdaQueryWrapper<BacBreakPoint> bacBreakPointLambdaQueryWrapper = new LambdaQueryWrapper<>();
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getDelFlag, 0);
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getGuidelines, "AUser-defined");
                List<BacBreakPoint> bacBreakPoints = bacBreakPointMapper.selectList(bacBreakPointLambdaQueryWrapper);
                List<String> serumGroupList = bacBreakPoints.stream().map(BacBreakPoint::getGenusGroup).collect(Collectors.toList());
                if (ToolsUtils.isNotEmpty(serumGroupList)) {
                    germDicts = germDicts.stream().filter(bg -> serumGroupList.contains(bg.getGenusGroup())).collect(Collectors.toList());
                } else {
                    germDicts = germDicts.stream().filter(bg -> Arrays.asList("!@#QWE").contains(bg.getGenusGroup())).collect(Collectors.toList());
                }
            }
            if (ToolsUtils.isNotEmpty(input.getFilterCondition()) && "3".equals(input.getFilterCondition())) {/*未维护 */
                LambdaQueryWrapper<BacBreakPoint> bacBreakPointLambdaQueryWrapper = new LambdaQueryWrapper<>();
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getDelFlag, 0);
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getGuidelines, "AUser-defined");
                List<BacBreakPoint> bacBreakPoints = bacBreakPointMapper.selectList(bacBreakPointLambdaQueryWrapper);
                List<String> serumGroupList = bacBreakPoints.stream().map(BacBreakPoint::getGenusGroup).collect(Collectors.toList());
                if (ToolsUtils.isNotEmpty(serumGroupList)) {
                    germDicts = germDicts.stream().filter(bg -> !serumGroupList.contains(bg.getGenusGroup())).collect(Collectors.toList());
                }
            }
            if (ToolsUtils.isNotEmpty(input.getKeyword()) && ToolsUtils.isNotEmpty(germDicts)) {
                germDicts = germDicts.stream().filter(bg -> Arrays.asList(input.getKeyword()).contains(bg.getGenusGroup())).collect(Collectors.toList());
            }
            if (ToolsUtils.isEmpty(germDicts)) {
                return Result.succ(1, "成功", res);
            }
            groupList = germDicts.stream().collect(Collectors.groupingBy(BacGermDict::getGenusGroup));
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "familyCode".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            germDicts = germDicts.stream().filter(bg -> ToolsUtils.isNotEmpty(bg.getFamilyCode())).collect(Collectors.toList());
            if (ToolsUtils.isNotEmpty(input.getFilterCondition()) && "2".equals(input.getFilterCondition())) {/*已 维护*/
                LambdaQueryWrapper<BacBreakPoint> bacBreakPointLambdaQueryWrapper = new LambdaQueryWrapper<>();
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getDelFlag, 0);
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getGuidelines, "AUser-defined");
                List<BacBreakPoint> bacBreakPoints = bacBreakPointMapper.selectList(bacBreakPointLambdaQueryWrapper);
                List<String> serumGroupList = bacBreakPoints.stream().map(BacBreakPoint::getFamilyCode).collect(Collectors.toList());
                if (ToolsUtils.isNotEmpty(serumGroupList)) {
                    germDicts = germDicts.stream().filter(bg -> serumGroupList.contains(bg.getFamilyCode())).collect(Collectors.toList());
                } else {
                    germDicts = germDicts.stream().filter(bg -> Arrays.asList("!@#QWE").contains(bg.getFamilyCode())).collect(Collectors.toList());
                }
            }
            if (ToolsUtils.isNotEmpty(input.getFilterCondition()) && "3".equals(input.getFilterCondition())) {/*未维护 */
                LambdaQueryWrapper<BacBreakPoint> bacBreakPointLambdaQueryWrapper = new LambdaQueryWrapper<>();
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getDelFlag, 0);
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getGuidelines, "AUser-defined");
                List<BacBreakPoint> bacBreakPoints = bacBreakPointMapper.selectList(bacBreakPointLambdaQueryWrapper);
                List<String> serumGroupList = bacBreakPoints.stream().map(BacBreakPoint::getFamilyCode).collect(Collectors.toList());
                if (ToolsUtils.isNotEmpty(serumGroupList)) {
                    germDicts = germDicts.stream().filter(bg -> !serumGroupList.contains(bg.getFamilyCode())).collect(Collectors.toList());
                }
            }
            if (ToolsUtils.isNotEmpty(input.getKeyword()) && ToolsUtils.isNotEmpty(germDicts)) {
                germDicts = germDicts.stream().filter(bg -> Arrays.asList(input.getKeyword()).contains(bg.getFamilyCode())).collect(Collectors.toList());
            }
            groupList = germDicts.stream().collect(Collectors.groupingBy(BacGermDict::getFamilyCode));
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "SubkingdomCode".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            germDicts = germDicts.stream().filter(bg -> ToolsUtils.isNotEmpty(bg.getSubKingdomCode())).collect(Collectors.toList());
            if (ToolsUtils.isNotEmpty(input.getFilterCondition()) && "2".equals(input.getFilterCondition())) {/*已 维护*/
                LambdaQueryWrapper<BacBreakPoint> bacBreakPointLambdaQueryWrapper = new LambdaQueryWrapper<>();
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getDelFlag, 0);
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getGuidelines, "AUser-defined");
                List<BacBreakPoint> bacBreakPoints = bacBreakPointMapper.selectList(bacBreakPointLambdaQueryWrapper);
                List<String> serumGroupList = bacBreakPoints.stream().map(BacBreakPoint::getSubkingdomCode).collect(Collectors.toList());
                if (ToolsUtils.isNotEmpty(serumGroupList)) {
                    germDicts = germDicts.stream().filter(bg -> serumGroupList.contains(bg.getSubKingdomCode())).collect(Collectors.toList());
                } else {
                    germDicts = germDicts.stream().filter(bg -> Arrays.asList("!@#QWE").contains(bg.getSubKingdomCode())).collect(Collectors.toList());
                }
            }
            if (ToolsUtils.isNotEmpty(input.getFilterCondition()) && "3".equals(input.getFilterCondition())) {/*未维护 */
                LambdaQueryWrapper<BacBreakPoint> bacBreakPointLambdaQueryWrapper = new LambdaQueryWrapper<>();
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getDelFlag, 0);
                bacBreakPointLambdaQueryWrapper.eq(BacBreakPoint::getGuidelines, "AUser-defined");
                List<BacBreakPoint> bacBreakPoints = bacBreakPointMapper.selectList(bacBreakPointLambdaQueryWrapper);
                List<String> serumGroupList = bacBreakPoints.stream().map(BacBreakPoint::getSubkingdomCode).collect(Collectors.toList());
                if (ToolsUtils.isNotEmpty(serumGroupList)) {
                    germDicts = germDicts.stream().filter(bg -> !serumGroupList.contains(bg.getSubKingdomCode())).collect(Collectors.toList());
                }
            }
            if (ToolsUtils.isNotEmpty(input.getKeyword()) && ToolsUtils.isNotEmpty(germDicts)) {
                germDicts = germDicts.stream().filter(bg -> Arrays.asList(input.getKeyword()).contains(bg.getSubKingdomCode())).collect(Collectors.toList());
            }
            groupList = germDicts.stream().collect(Collectors.groupingBy(BacGermDict::getSubKingdomCode));
        }
        //germDicts.stream().collect(Collectors.groupingBy())
        if (CollectionUtils.isEmpty(groupList)) {
            return Result.succ(1, "查询", new ArrayList<>());
        }
        /*血清型*/
        List<Map<String, Object>> serumGroupList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("serumGroup").getData();
        /*种分组*/
        List<Map<String, Object>> speciesGroupList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("speciesGroup").getData();
        /*属编码*/
        List<Map<String, Object>> genusCodeList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("genusCode").getData();
        /*属分组*/
        List<Map<String, Object>> genusGroupList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("genusGroup").getData();
        /*科编码*/
        List<Map<String, Object>> familyCodeList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("familyCode").getData();
        /*亚界*/
        List<Map<String, Object>> SubkingdomCodeList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("SubkingdomCode").getData();

        /*循环赋值*/
        for (Map.Entry<String, List<BacGermDict>> entry : groupList.entrySet()) {
            BreakPointMapperCategoryDto breakPointMapperCategoryDto = new BreakPointMapperCategoryDto();
            breakPointMapperCategoryDto.setCateGoryCode(input.getCateGoryCode());
            breakPointMapperCategoryDto.setCode(entry.getKey());
            String Name = "";
            switch (input.getCateGoryCode()) {
                case "serumGroup":
                    serumGroupList.stream().filter(a -> a.get("code").equals(entry.getKey())).findFirst().ifPresent(stringObjectMap -> breakPointMapperCategoryDto.setName((String) stringObjectMap.get("name")));
                    break;
                case "speciesGroup":
                    speciesGroupList.stream().filter(a -> a.get("code").equals(entry.getKey())).findFirst().ifPresent(stringObjectMap -> breakPointMapperCategoryDto.setName((String) stringObjectMap.get("name")));
                    break;
                case "genusCode":
                    Optional<Map<String, Object>> code = genusCodeList.stream().filter(a -> a.get("code").equals(entry.getKey())).findFirst();
                    if(code.isPresent()){
                        code.ifPresent(stringObjectMap -> breakPointMapperCategoryDto.setName((String) stringObjectMap.get("name")));
                    }else {
                        breakPointMapperCategoryDto.setName(entry.getKey());
                    }

                    break;
                case "genusGroup":
                    genusGroupList.stream().filter(a -> a.get("code").equals(entry.getKey())).findFirst().ifPresent(stringObjectMap -> breakPointMapperCategoryDto.setName((String) stringObjectMap.get("name")));
                    break;
                case "familyCode":
                    familyCodeList.stream().filter(a -> a.get("code").equals(entry.getKey())).findFirst().ifPresent(stringObjectMap -> breakPointMapperCategoryDto.setName((String) stringObjectMap.get("name")));
                    break;
                case "SubkingdomCode":
                    SubkingdomCodeList.stream().filter(a -> a.get("code").equals(entry.getKey())).findFirst().ifPresent(stringObjectMap -> breakPointMapperCategoryDto.setName((String) stringObjectMap.get("name")));
                    break;
                case "WHONETORGCODE":
                    List<BacGermDict> stringObjectMap = entry.getValue();
                    breakPointMapperCategoryDto.setName(ToolsUtils.isNotEmpty(stringObjectMap) ? stringObjectMap.get(0).getGermNa() : entry.getKey());
                    break;
            }
            res.add(breakPointMapperCategoryDto);
        }
        return Result.succ(1, "查询成功", res);
    }

    @Override
    public Result findGermDictListByCode(BreakPointMapperCategoryParam input) {
        if (ToolsUtils.isEmpty(input.getBreakPointMapperCategoryDto())) {
            return Result.succ(1, "查询成功", new ArrayList<>());
        }
        Map<String, List<BacGermDict>> groupList = null;
        LambdaQueryWrapper<BacGermDict> bacGermDictLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bacGermDictLambdaQueryWrapper.eq(BacGermDict::getDelFlag, 0);

        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "serumGroup".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            bacGermDictLambdaQueryWrapper.eq(BacGermDict::getSerumGroup, input.getBreakPointMapperCategoryDto().getCode());
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "speciesGroup".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            bacGermDictLambdaQueryWrapper.eq(BacGermDict::getSpeciesGroup, input.getBreakPointMapperCategoryDto().getCode());
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "genusCode".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            bacGermDictLambdaQueryWrapper.eq(BacGermDict::getGenusCode, input.getBreakPointMapperCategoryDto().getCode());
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "genusGroup".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            bacGermDictLambdaQueryWrapper.eq(BacGermDict::getGenusGroup, input.getBreakPointMapperCategoryDto().getCode());
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "familyCode".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            bacGermDictLambdaQueryWrapper.eq(BacGermDict::getFamilyCode, input.getBreakPointMapperCategoryDto().getCode());
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "SubkingdomCode".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            bacGermDictLambdaQueryWrapper.eq(BacGermDict::getSubKingdomCode, input.getBreakPointMapperCategoryDto().getCode());
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "WHONETORGCODE".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            bacGermDictLambdaQueryWrapper.eq(BacGermDict::getGermNo, input.getBreakPointMapperCategoryDto().getCode());
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "ANAEROBE".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            if (ToolsUtils.isNotEmpty(input.getBreakPointMapperCategoryDto().getCode()) && "ANA".equals(input.getBreakPointMapperCategoryDto().getCode())) {
                bacGermDictLambdaQueryWrapper.eq(BacGermDict::getIsAnaerobe, 1);
            }
        }
        if (ToolsUtils.isNotEmpty(input.getKeyword())) {
            bacGermDictLambdaQueryWrapper.and(p ->
                    p.like(BacGermDict::getGermNa, input.getKeyword())
                            .or()
                            .like(BacGermDict::getGermNo, input.getKeyword())
                            .or().
                            like(BacGermDict::getStandardNa, input.getKeyword())
                            .or()
                            .like(BacGermDict::getInputCode, input.getKeyword()));
        }

        List<BacGermDict> germDicts = bacGermDictMapper.selectList(bacGermDictLambdaQueryWrapper);
        return Result.succ(1, "成功", germDicts);
    }

    @Override
    public Result findBreakPointListByCode(BreakPointMapperCategoryParam input) {
        if (ToolsUtils.isEmpty(input.getBreakPointMapperCategoryDto())) {
            return Result.succ(1, "查询成功", new ArrayList<>());
        }
        Map<String, List<BacBreakPoint>> groupList = null;
        MPJLambdaWrapper<BacBreakPoint> bacGermDictLambdaQueryWrapper = new MPJLambdaWrapper<>();
        bacGermDictLambdaQueryWrapper.selectAll(BacBreakPoint.class);
        bacGermDictLambdaQueryWrapper.selectAs(BacAntibioticsDict::getAntName, BacBreakPoint::getAntName);
        bacGermDictLambdaQueryWrapper.leftJoin(BacAntibioticsDict.class, BacAntibioticsDict::getAntCode, BacBreakPoint::getWhonetAbxCode);
        bacGermDictLambdaQueryWrapper.eq(BacBreakPoint::getDelFlag, 0);
        bacGermDictLambdaQueryWrapper.eq(BacBreakPoint::getGuidelines, "AUser-defined");
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "serumGroup".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            bacGermDictLambdaQueryWrapper.eq(BacBreakPoint::getSerovarGroup, input.getBreakPointMapperCategoryDto().getCode());
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "speciesGroup".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            bacGermDictLambdaQueryWrapper.eq(BacBreakPoint::getSpeciesGroup, input.getBreakPointMapperCategoryDto().getCode());
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "genusCode".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            bacGermDictLambdaQueryWrapper.eq(BacBreakPoint::getGenusCode, input.getBreakPointMapperCategoryDto().getCode());
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "genusGroup".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            bacGermDictLambdaQueryWrapper.eq(BacBreakPoint::getGenusGroup, input.getBreakPointMapperCategoryDto().getCode());
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "familyCode".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            bacGermDictLambdaQueryWrapper.eq(BacBreakPoint::getFamilyCode, input.getBreakPointMapperCategoryDto().getCode());
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "SubkingdomCode".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            bacGermDictLambdaQueryWrapper.eq(BacBreakPoint::getSubkingdomCode, input.getBreakPointMapperCategoryDto().getCode());
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "WHONETORGCODE".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            bacGermDictLambdaQueryWrapper.eq(BacBreakPoint::getWhonetOrgCode, input.getBreakPointMapperCategoryDto().getCode());
        }
        if (ToolsUtils.isNotEmpty(input.getCateGoryCode()) && "ANAEROBE".toUpperCase().equals(input.getCateGoryCode().toUpperCase())) {
            if (ToolsUtils.isNotEmpty(input.getBreakPointMapperCategoryDto().getCode()) && "ANA".equals(input.getBreakPointMapperCategoryDto().getCode())) {
                bacGermDictLambdaQueryWrapper.eq(BacBreakPoint::getAnaerobe, "ANA");
            }
        }
        if (ToolsUtils.isNotEmpty(input.getKeyword())) {
            bacGermDictLambdaQueryWrapper.and(p ->
                    p.like(BacBreakPoint::getWhonetOrgCode, input.getKeyword())
                            .or()
                            .like(BacBreakPoint::getSerovarGroup, input.getKeyword())
                            .or().
                            like(BacBreakPoint::getSpeciesGroup, input.getKeyword())
                            .or()
                            .like(BacBreakPoint::getGenusCode, input.getKeyword())
                            .or()
                            .like(BacBreakPoint::getGenusGroup, input.getKeyword())
                            .or()
                            .like(BacBreakPoint::getFamilyCode, input.getKeyword())
                            .or()
                            .like(BacBreakPoint::getSubkingdomCode, input.getKeyword())
                            .or()
                            .like(BacBreakPoint::getAnaerobe, input.getKeyword()));
        }

        List<BacBreakPoint> germDicts = bacBreakPointMapper.selectList(bacGermDictLambdaQueryWrapper);
        return Result.succ(1, "成功", germDicts);
    }

    @Override
    public Result batchUpdateBreakPoint(BreakPointDto vo) {
        List<BacBreakPoint> bacBreakPointList = vo.getBacBreakPointList();
        boolean b = this.saveOrUpdateBatch(bacBreakPointList);
        if (b) {
            return Result.succ(1, "修改成功", null);
        } else {
            return Result.fail(400, "修改失败", "");
        }
    }

    @Override
    public Result getPointLogList(Params params) {
        Page<BacBreakPointLog> page = new Page<>();
        page.setCurrent(params.getPageIndex());
        page.setSize(params.getPageSize());

        MPJLambdaWrapper<BacBreakPointLog> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(BacBreakPointLog.class);
        queryWrapper.selectAs("test", SysUser::getRealname, BacBreakPointLog::getCreatorName);
        queryWrapper.eq(BacBreakPointLog::getDelFlag, 0);
        queryWrapper.leftJoin(SysUser.class, "test", p -> p.eq(SysUser::getId, BacBreakPointLog::getCreator).eq(SysUser::getDelFlag, 0).isNotNull(SysUser::getRealname).ne(SysUser::getRealname, ""));

        Page<BacBreakPointLog> bacBreakPointPage = bacBreakPointLogMapper.selectPage(page, queryWrapper);
        return Result.succ(1, "查询成功", bacBreakPointPage);
    }


}
